package com.example.quizamination

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.navigation.Navigation
import androidx.navigation.testing.TestNavHostController
import androidx.test.annotation.UiThreadTest
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.example.quizamination.quiz.QuizStorage
import com.example.quizamination.ui.main.MainFragment
import com.example.quizamination.ui.main.ResultFragment
import com.example.quizamination.ui.main.WelcomeFragment
import junit.framework.TestCase.assertNotNull
import org.hamcrest.CoreMatchers.not
import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainFragmentUnitTest {

    @Test
    fun testQuizLocaleSelection() {
        val locale = QuizStorage.Locale.En
        val quiz = QuizStorage.getQuiz(locale)
        assertNotNull(quiz)
        assertEquals(locale, QuizStorage.Locale.En)
    }

    @Test
    fun testAnswersInitialization() {
        val quiz = QuizStorage.getQuiz(QuizStorage.Locale.En)
        val answers = MutableList(quiz.questions.size) { -1 }
        assertEquals(answers.size, quiz.questions.size)
        assertTrue(answers.all { it == -1 })
    }

    @Test
    fun testQuizRetrieval() {
        val quiz = QuizStorage.getQuiz(QuizStorage.Locale.En)
        assertNotNull(quiz)
        assertTrue(quiz.questions.isNotEmpty())
    }

    @Test
    fun testResultFragmentDisplaysCorrectText() {
        val scenario = ActivityScenario.launch(MainActivity::class.java)

        scenario.onActivity { activity ->
            val resultFragment = ResultFragment().apply {
                arguments = Bundle().apply {
                    putString("resultText", "Test result")
                }
            }

            activity.supportFragmentManager.beginTransaction()
                .replace(android.R.id.content, resultFragment)
                .commitNow()

            activity.supportFragmentManager.executePendingTransactions()
            val resTextView = resultFragment.view?.findViewById<TextView>(R.id.resTextView)

            assertEquals("Test result", resTextView?.text)
        }
    }

    @Test
    fun testNavigationToHomeFragment() {
        val navController = TestNavHostController(
            ApplicationProvider.getApplicationContext()
        )

        val scenario = ActivityScenario.launch(MainActivity::class.java)
        scenario.onActivity { activity ->
            {
                val fragment = WelcomeFragment()

                navController.setGraph(R.navigation.main_graph)

                Navigation.setViewNavController(fragment.requireView(), navController)

                onView(withId(R.id.nextButton)).perform(click())
                assertEquals(navController.currentDestination, R.id.mainFragment)
            }
        }
    }
}